﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class AdminList : System.Web.UI.Page
    {

        public Member _admin = new Member();
        protected General _general = new General();
        protected DataTable _dttAdmin;
        

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Role Admin
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionPrincipal).IndexOf(Session["role"].ToString() + ",") != -1)
            {

                
                try
                {
                    if (!IsPostBack)
                    {
                        if (Request.QueryString["hidAdminVisible"] != null)
                        {
                            //Set a teacher is visible or not
                            if (Request.QueryString["hidAdminVisible"].ToString() == "true")
                            {
                                _admin.SetMemberVisible(Request.QueryString["hidAdminID"].ToString(), "0");
                            }
                            else if (Request.QueryString["hidAdminVisible"].ToString() == "false")
                            {
                                _admin.SetMemberVisible(Request.QueryString["hidAdminID"].ToString(), "1");
                            }
                        }

                        //Change Permission
                        if (Request.QueryString["hidAdminPermission"] != null)
                        {
                            if (!_admin.SetAdminRole(Request.QueryString["hidAdminID"].ToString(), Request.QueryString["hidAdminPermission"].ToString()))
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống. Xin vui long làm lại.')", true);
                            }
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã thay đổi quyền')", true);
                        }

                        //Reset Pass
                        if (Request.QueryString["hidAdminResetPass"] != null)
                        {
                            _admin.ResetDefaultPass(Request.QueryString["hidAdminResetPass"].ToString());
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã tạo lại mật khẩu mặc định.')", true);
                        }

                        if (Request.Cookies[ConstantClass.cooAdminList_ddlStatus] == null)
                        {
                            HttpCookie cookie = new HttpCookie(ConstantClass.cooAdminList_ddlStatus, "1");
                            Response.Cookies.Add(cookie);
                            cookie.Expires = DateTime.Now.AddDays(1);
                        }
                        _dttAdmin = _admin.GetAdminList(Request.Cookies[ConstantClass.cooAdminList_ddlStatus].Value, Session["memberID"].ToString());
                        ddlShowStatus.SelectedValue = Request.Cookies[ConstantClass.cooAdminList_ddlStatus].Value;

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }

            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }

        }


        protected void ddlShowStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _dttAdmin = _admin.GetAdminList(ddlShowStatus.SelectedValue, Session["memberID"].ToString());

                if (Request.Cookies[ConstantClass.cooAdminList_ddlStatus] == null)
                {
                    HttpCookie cookie = new HttpCookie(ConstantClass.cooAdminList_ddlStatus, ddlShowStatus.SelectedValue);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    Response.Cookies[ConstantClass.cooAdminList_ddlStatus].Value = ddlShowStatus.SelectedValue;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
    }
}